package me.numark.model;

import com.google.gson.Gson;

import android.content.ContentValues;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;

import java.util.Date;

import me.numark.providers.Numa;
import me.numark.view.UserClickSpan;

/**
 * Created by tian on 14/11/6:上午8:18.
 */
public class Feed {

    public String c_type;

    public int mid;

    public int type;

    public int status;

    public String description;

    public int nsid;

    public String create;

    public int comments;

    public int forward;

    public int like;

    public long create_timestamp;

    public User user;

    public int id;

    public News news;

    public boolean is_like;

    public User[] forward_person;

    public static final int FEED_TYPE_MINE = 0;

    public static final int FEED_TYPE_HOT = 1;

    public boolean is_mine;

    public Topic topic;

    public static SpannableStringBuilder getForwardPersonText(User[] forward_person) {
        SpannableStringBuilder sb = new SpannableStringBuilder();
        int count = 0;
        String delimiter = "，";
        for (User person : forward_person) {
            if (count == 3) {
                break;
            }
            if (count > 0)
                sb.append(delimiter);
            SpannableString userStr = new SpannableString(person.nick);
            UserClickSpan span = new UserClickSpan(person);
            userStr.setSpan(span, 0, userStr.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            sb.append(userStr);
            count++;
        }
        sb.append("转发");
        return sb;
    }

    public static ContentValues feedToContentValues(Feed feed) {
        ContentValues cv = new ContentValues();
        cv.put(Numa.FeedColumns.FEED_ID, feed.id);
        cv.put(Numa.FeedColumns.FEED_TIME, feed.create_timestamp);
        cv.put(Numa.FeedColumns.CREATE_AT, feed.create);
        cv.put(Numa.FeedColumns.CONTENT, feed.toString());
        return cv;
    }

    @Override
    public String toString() {
        Gson gson = new Gson();
        return gson.toJson(this);
    }

    public Date getDate() {
        return new Date(create_timestamp * 1000);
    }

    public static Feed fromString(String string) {
        return new Gson().fromJson(string, Feed.class);
    }
}
